package com.ynet.middleground.approve.service.impl;

import java.util.List;

import javax.validation.Valid;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import com.google.common.base.Throwables;
import com.ynet.core.common.BaseReqObj;
import com.ynet.core.common.BaseReqObjPager;
import com.ynet.core.common.ServiceResult;
import com.ynet.core.exception.BusinessException;
import com.ynet.core.log.IfpLogger;
import com.ynet.middleground.approve.bean.AddTaskAttrReq;
import com.ynet.middleground.approve.bean.QueryTaskAttrReq;
import com.ynet.middleground.approve.bean.UpdateTaskAttrReq;
import com.ynet.middleground.approve.dto.TaskAttrDTO;
import com.ynet.middleground.approve.model.TaskAttrModel;
import com.ynet.middleground.approve.service.TaskAttrService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * @author liulx
 * @description 流程任务节点业务属性接口实现类
 * @date 2020-06-29 15:09
 */
@Api(value = "流程节点业务属性相关接口服务", tags = {"流程节点业务属性相关接口服务"})
@Produces({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Consumes({MediaType.APPLICATION_JSON + "; " + "charset=UTF-8"})
@Path("task-attr")
@Service(timeout = 6000, version = "0.0.1")
public class TaskAttrServiceImpl implements TaskAttrService {

    @Autowired
    TaskAttrModel taskAttrModel;

    /**
     * 查询所有流程节点业务属性
     *
     * @param reqObj 基础请求参数
     * @return 流程任务节点业务属性列表
     */
    @POST
    @ApiOperation(value = "查询所有流程节点业务属性",
        notes = "查询所有流程节点业务属性，节点业务属性会在任务信息的 variables 字段中，且出现业务属性字段，值均为 true，渠道自行修改除外", produces = "application/json")
    @ApiImplicitParam(name = "requestObj", dataType = "BaseReqObj", paramType = "body", required = true)
    @Override
    public ServiceResult<List<TaskAttrDTO>> list(@Valid BaseReqObj reqObj) {
        ServiceResult<List<TaskAttrDTO>> result = new ServiceResult<>();
        try {
            List<TaskAttrDTO> resultList = taskAttrModel.listTaskAttr();
            result.setResult(resultList);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "查询所有流程节点业务属性异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 分页查询流程节点业务属性 <br>
     * 目前为内管使用，接口文档不体现
     * 
     * @param reqObj 分页基础请求参数
     * @return 流程任务节点业务属性列表
     */
    @ApiOperation(value = "分页查询流程节点业务属性", notes = "分页查询流程节点业务属性", produces = "application/json", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "BaseReqObj", paramType = "body", required = true)
    @Override
    public ServiceResult<List<TaskAttrDTO>> listPage(@Valid BaseReqObjPager reqObj) {
        ServiceResult<List<TaskAttrDTO>> result = new ServiceResult<>();
        try {
            List<TaskAttrDTO> resultList = taskAttrModel.listPage(reqObj);
            result.setResult(resultList);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "分页查询流程节点业务属性异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        result.setPagerInfo(reqObj.getPagerInfo());
        return result;
    }

    /**
     * 根据 id 查询审批节点业务属性信息 <br>
     * 目前为内管使用，接口文档不体现
     *
     * @param req 查询审批节点业务属性详情请求信息
     * @return 业务属性传输对象
     */
    @ApiOperation(value = "根据 id 查询审批节点业务属性信息", notes = "根据 id 查询审批节点业务属性信息", produces = "application/json",
        hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "QueryTaskAttrReq", paramType = "body", required = true)
    @Override
    public ServiceResult<TaskAttrDTO> getById(@Valid QueryTaskAttrReq req) {
        ServiceResult<TaskAttrDTO> result = new ServiceResult<>();
        try {
            result.setResult(taskAttrModel.getById(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "根据 id 查询审批节点业务属性信息异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 根据 id 更新业务属性信息 <br>
     * 目前为内管使用，接口文档不体现
     *
     * @param req 修改审批节点业务属性请求信息
     * @return 修改成功以 success 字段为准
     */
    @ApiOperation(value = "根据 id 更新业务属性信息", notes = "根据 id 更新业务属性信息", produces = "application/json", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "UpdateTaskAttrReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> modifyById(@Valid UpdateTaskAttrReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            taskAttrModel.modifyById(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "根据 id 更新业务属性信息异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 新增审批节点业务属性<br>
     * 目前为内管使用，接口文档不体现
     *
     * @param req 新增审批节点业务属性请求信息
     * @return 新增后的业务属性 id
     */
    @ApiOperation(value = "新增审批节点业务属性", notes = "新增审批节点业务属性", produces = "application/json", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "AddTaskAttrReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Integer> add(@Valid AddTaskAttrReq req) {
        ServiceResult<Integer> result = new ServiceResult<>();
        try {
            result.setResult(taskAttrModel.add(req));
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "新增审批节点业务属性异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

    /**
     * 根据业务属性 id 删除信息<br>
     * 目前为内管使用，接口文档不体现
     *
     * @param req 查询审批节点业务属性详情请求信息
     * @return 删除成功以 success 字段为准
     */
    @ApiOperation(value = "删除审批节点业务属性", notes = "删除审批节点业务属性", produces = "application/json", hidden = true)
    @ApiImplicitParam(name = "requestObj", dataType = "QueryTaskAttrReq", paramType = "body", required = true)
    @Override
    public ServiceResult<Object> deleteById(@Valid QueryTaskAttrReq req) {
        ServiceResult<Object> result = new ServiceResult<>();
        try {
            taskAttrModel.deleteById(req);
        } catch (BusinessException be) {
            result.setError(be.getCode(), be.getMessage());
        } catch (Exception e) {
            IfpLogger.error(Thread.currentThread().getStackTrace()[1].getMethodName(), "删除审批节点业务属性异常,case:{}",
                Throwables.getStackTraceAsString(e));
            result.setError("ECAP0000", "服务异常，请联系系统管理员");
            return result;
        }
        return result;
    }

}
